package gd.qaq.blue.locationleader.cmd;

import gd.qaq.blue.locationleader.LocationLeader;
import gd.qaq.blue.locationleader.api.API;
import gd.qaq.blue.locationleader.entity.TargetLocation;
import gd.qaq.blue.locationleader.listener.LocationLeaderListener;
import gd.qaq.blue.locationleader.util.pathfinder.PathUtils;
import gd.qaq.blue.locationleader.util.pathfinder.Tile;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static gd.qaq.blue.locationleader.LocationLeader.*;
import static gd.qaq.blue.locationleader.api.API.getLocationPath;
import static gd.qaq.blue.locationleader.listener.LocationLeaderListener.checkIsArrived;
import static gd.qaq.blue.locationleader.listener.LocationLeaderListener.playerUUIDAndTarget;

public class LocationLeaderCommands implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        int argsLen = args.length;
        if (argsLen == 0 || (argsLen == 1 && args[0].equalsIgnoreCase("help"))) {
            help(sender);
            return true;
        }
        if (argsLen == 1) {
            if (args[0].equalsIgnoreCase("reload")) {
                plugin.reload(sender);
                return true;
            }
//            } else if (args[0].equalsIgnoreCase("auto")) {
//                if (sender instanceof Player) {
//                    Player player = (Player) sender;
//                    TargetLocation targetLocation = playerUUIDAndTarget.get(player.getUniqueId().toString());
//                    if (targetLocation == null) {
//                        error(player, "你还没有目的地!");
//                        return true;
//                    }
//                    API.startAutoFindPath(player, targetLocation);
//                    return true;
//                }
//            } else if (args[0].equalsIgnoreCase("cancel")) {
//                if (sender instanceof Player) {
//                    Player player = (Player) sender;
//                    sendSubtitleToPlayer(player, "", 1);
//                    TargetLocation targetLocation = playerUUIDAndTarget.get(player.getUniqueId().toString());
//                    if (targetLocation == null) {
//                        error(player, "你还没有目的地!");
//                        return true;
//                    }
//                    targetLocation.setAuto(false);
//                    success(player, "已取消自动寻路");
//                    return true;
//                }
//            }
        }
        if (argsLen == 2 && sender instanceof Player && sender.isOp()) {
            String arg1 = args[0];
            String arg2 = args[1];
            Player player = (Player) sender;
            if (arg1.equalsIgnoreCase("add")) {
                if (jdbcUtils.add(arg2, player.getLocation())) {
                    success(player, "已添加新的目的地:&f " + arg2);
                } else {
                    error(player, "添加目的地失败!");
                }
                return true;
            } else if (arg1.equalsIgnoreCase("remove")) {
                if (jdbcUtils.remove(arg2)) {
                    success(player, "已删除目的地:&f " + arg2);
                } else {
                    error(player, "删除目的地失败!没有名为&f" + arg2 + "&c的目的地!");
                }
                return true;
            }
        }
        if (argsLen == 3) {
            if (sender.isOp()) {
                String arg1 = args[0];
                String arg2 = args[1];
                String arg3 = args[2];
                Player player = Bukkit.getPlayer(arg2);
                if (player != null && player.isOnline()) {
                    if (arg1.equalsIgnoreCase("set")) {
                        Map locationMap = jdbcUtils.get(arg3);
                        if (locationMap == null || locationMap.size() == 0) {
                            error(sender, "设置目的地失败!没有名为&f" + arg3 + "&c的目的地!");
                        } else {
                            Location location = (Location) locationMap.get("location");
                            if (!player.getWorld().getName().equals(location.getWorld().getName())) {
                                error(sender, "设置目的地失败!玩家与目的地不在同一世界!");
                                return true;
                            }
                            player.sendTitle(colorFormat("&b"), "", 0, 10000000, 0);

                            List<Location> locations = getLocationPath(player, location);

                            sendSubtitleToPlayer(player, colorFormat(config.getString("plugin.lang.newTarget").replace("%target%", arg3)));
//                            player.setCompassTarget(location);
                            TargetLocation targetLocation = new TargetLocation();
                            targetLocation.setLocation(location);
                            targetLocation.setLocationName(arg3);
                            targetLocation.setRoutes(locations);
                            jdbcUtils.add(player.getUniqueId().toString(), arg3, targetLocation.getLocation());
                            LocationLeaderListener.playerUUIDAndTarget.put(player.getUniqueId().toString(), targetLocation);
                        }
                        return true;
                    }
                } else {
                    error(sender, "玩家不在线!");
                    return true;
                }
            }
        }
        error(sender, "参数有误!");
        return true;
    }


    private void help(CommandSender player) {
        StringBuilder helpContent = new StringBuilder();
        helpContent.append("&6&l-----------&bLocationLeader&6&l------------\n");
        helpContent.append("&b(插件指令可以简化为ll或locl)\n");
        helpContent.append("&b/locationLeader help &e查看帮助\n");
        if (player.isOp()) {
            helpContent.append("&b/locationLeader add <locationName> &e添加目的地\n");
            helpContent.append("&b/locationLeader remove <locationName> &e删除目的地\n");
            helpContent.append("&b/locationLeader set <playerName> <locationName> &e设置玩家目的地\n");
            helpContent.append("&b/locationLeader reload &e重载插件\n");
        }
        helpContent.append("&6&l--------------------------------");
        player.sendMessage(colorFormat(helpContent.toString()));
    }
}
